package cn.bdqn.t354;

import cn.bdqn.t354.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisTester {
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    RedisTemplate redisTemplate;

    @Test
    public void testString(){
        ValueOperations<String, String> sso =
                stringRedisTemplate.opsForValue();

        BoundValueOperations<String, String> mystring2 =
                stringRedisTemplate.boundValueOps("mystring2");
        mystring2.set("123");
        mystring2.get();


        stringRedisTemplate.boundHashOps("myhash2");
        stringRedisTemplate.boundListOps("mylist2");
        stringRedisTemplate.boundSetOps("myset2");
        stringRedisTemplate.boundZSetOps("myzset2") ;

        //1.存储
        sso.set("aaaaaaaaa","ssssssssssssssss");
        //设置了有效期
        sso.set("bbbbbbbbbbbb","ssssssdsdsds",10,
                TimeUnit.SECONDS);
        //2.获取数据
        String str = sso.get("aaaaaaaaa");
        System.out.println(str);

        //3.删除
        stringRedisTemplate.delete("aaaaaaaaa");

        //4.自增或者自减
        sso.set("bbb","1");
        sso.increment("bbb",2);
        sso.decrement("bbb",10);
        //如果不指定自增或者自减多少 默认加1或者减1
        sso.increment("bbb");

        ValueOperations vor = redisTemplate.opsForValue();
        vor.set("user",new User());


    }

    @Test
    public void testHash() {
        HashOperations<String, Object, Object> soo =
                stringRedisTemplate.opsForHash();

        soo.put("bbbbsdsd", "username", "yuoxi");
        soo.put("bbbbsdsd", "age", "19");

        System.out.println(soo.get("bbbbsdsd", "username"));

        soo.delete("bbbbsdsd", "username");

    }
}
